<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
    <script src="./utils/createDraw.js"></script>
    <script src="./utils/LineString.js"></script>
</head>

<body>
    <button onclick="addLine()">画笔-点击线</button>
    <div id="map">
    </div>
    <script>
        var docLayer = new Zondy.Map.Doc("", "xzd_line", {

        })
        var map = new ol.Map({
            target: "map",
            layers: [gaode, docLayer],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })
        /* 1、激活画笔-获取坐标 */
        /* 构建画布--空的图层 */
        var source = new ol.source.Vector({})
        var layer = new ol.layer.Vector({
            source
        })
        map.addLayer(layer);
        let draw = null;
        /* 请你说一下JavaScript的垃圾回收机制 */
        function addLine() {
            draw = createDraw({
                source,
                type: "LineString",
                callback: handleDraw
            })
            /* 激活 */
            map.addInteraction(draw);
        }
        function handleDraw(evt) {
            var position = evt.feature.getGeometry().getCoordinates();
            var attr = [];
            var service  ={
                name:"xzd_line",
                layerId:1
            }
            /* 调用中地接口-添加线要素 */
            LineString.add({
                position,
                attr,
                service,
                docLayer
            })
            source.clear();
            map.removeInteraction(draw);
        }
    </script>
</body>

</html>